package com.jf.cloud.product.service.impl;

import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.util.PageUtil;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.product.model.SpuRank;
import com.jf.cloud.product.mapper.SpuRankMapper;
import com.jf.cloud.product.service.SpuRankService;
import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 
 *
 * @author zz
 * @date 2023-08-14 10:34:19
 */
@Service
public class SpuRankServiceImpl implements SpuRankService {

    @Autowired
    private SpuRankMapper spuRankMapper;

    @Override
    public PageVO<SpuRank> page(PageDTO pageDTO) {
        return PageUtil.doPage(pageDTO, () -> spuRankMapper.list());
    }

    @Override
    public List<SpuRank> list(Long spuId, Integer status,Long skuId) {
        return spuRankMapper.listAll(spuId,status,skuId);
    }
    @Override
    public List<SpuRank> listAll(Long spuId,Long skuId) {
        return spuRankMapper.listAllRank(spuId,skuId);
    }

    @Override
    public List<SpuRank> getByOrderId(Long orderId) {
        return spuRankMapper.getByOrderId(orderId);
    }

    @Override
    public boolean checkStatus(String ids) {
        boolean flag=true;
        for (int i = 0; i <ids.split(",").length ; i++) {
           SpuRank spuRank= spuRankMapper.getById(Long.valueOf(ids.split(",")[i]));
           if(spuRank.getStatus()==2){
               flag=false;
               break;
           }
        }
        if(flag){
            for (int i = 0; i <ids.split(",").length ; i++) {
                SpuRank spuRank= spuRankMapper.getById(Long.valueOf(ids.split(",")[i]));
                spuRank.setStatus(1);
                spuRankMapper.update(spuRank);
            }
        }
        return flag;
    }


    @Override
    public SpuRank getById(Long id) {
        return spuRankMapper.getById(id);
    }

    @Override
    public void save(SpuRank spuRank) {
        spuRankMapper.save(spuRank);
    }

    @Override
    public void update(SpuRank spuRank) {
        spuRankMapper.update(spuRank);
    }

    @Override
    public void deleteById(Long id) {
        spuRankMapper.deleteById(id);
    }
}
